const Pick = require("../template")

function testPickBasic() {
  console.log(
    "1. 基本对象选择测试:",
    (() => {
      const object = { a: 1, b: 2, c: 3, d: 4 }
      const result = Pick.pick(object, 'a', 'c')
      const expected = { a: 1, c: 3 }
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "2. 数组路径选择测试:",
    (() => {
      const object = { a: 1, b: 2, c: 3, d: 4 }
      const result = Pick.pick(object, ['a', 'b'])
      const expected = { a: 1, b: 2 }
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "3. 深度路径选择测试:",
    (() => {
      const object = { user: { name: 'John', age: 30 }, data: { value: 100 } }
      const result = Pick.pickDeep(object, 'user.name', 'data.value')
      const expected = { user: { name: 'John' }, data: { value: 100 } }
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "4. 条件选择测试:",
    (() => {
      const object = { a: 1, b: 2, c: 3, d: 4 }
      const result = Pick.pickBy(object, (value) => value % 2 === 0)
      const expected = { b: 2, d: 4 }
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "5. 类型选择测试:",
    (() => {
      const object = { a: 1, b: 'hello', c: 3, d: 'world' }
      const result = Pick.pickByType(object, 'string')
      const expected = { b: 'hello', d: 'world' }
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "6. 正则选择测试:",
    (() => {
      const object = { user_name: 'John', user_age: 30, data_value: 100 }
      const result = Pick.pickByPattern(object, /^user_/)
      const expected = { user_name: 'John', user_age: 30 }
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "7. 默认值选择测试:",
    (() => {
      const object = { a: 1, b: 2 }
      const result = Pick.pickWithDefaults(object, 'a', 'c', { c: 0 })
      const expected = { a: 1, c: 0 }
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "8. 转换选择测试:",
    (() => {
      const object = { a: 1, b: 2, c: 3 }
      const result = Pick.pickWithTransform(object, 'a', 'b', { a: (val) => val * 2 })
      const expected = { a: 2, b: 2 }
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "9. 验证选择测试:",
    (() => {
      const object = { name: 'John', age: 30, email: 'john@example.com' }
      const result = Pick.pickWithValidation(object, 'name', 'age', {
        name: 'string',
        age: 'number'
      })
      const expected = { name: 'John', age: 30 }
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "10. 空对象选择测试:",
    (() => {
      const object = {}
      const result = Pick.pick(object, 'a', 'b')
      return Object.keys(result).length === 0
    })()
  )

  console.log(
    "11. this上下文选择测试:",
    (() => {
      const pickObj = {
        multiplier: 2,
        processPick: function(object) {
          const picked = Pick.pick(object, 'a', 'b')
          return Object.keys(picked).length * this.multiplier
        }
      }
      const object = { a: 1, b: 2, c: 3 }
      return pickObj.processPick(object) === 4 // 2*2=4
    })()
  )

  console.log(
    "12. 复杂对象this绑定选择测试:",
    (() => {
      const pickConfigObj = {
        config: { threshold: 2 },
        processWithConfig: function(object) {
          const picked = Pick.pick(object, 'a', 'b')
          return Object.keys(picked).length >= this.config.threshold
        }
      }
      const object = { a: 1, b: 2, c: 3 }
      return pickConfigObj.processWithConfig(object) === true
    })()
  )
}

testPickBasic()
